package cn.iocoder.yudao.framework.mybatis.interceptor;

import cn.iocoder.yudao.framework.mybatis.component.MinPartitionDateComponent;
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.springframework.util.StringUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * TODO
 *
 * @className: SqlPmValueInterceptor
 * @author: tangkh
 * @date: 2023-09-19 16:32
 */
public class SqlPmValueInterceptor implements InnerInterceptor {
    private static String pointReg = "/\\*#(.+?)'\\((.+?)\\)'(\\s*)#\\*/";
    private static Pattern p;
    private static MinPartitionDateComponent minPartitionDateComponent;

    public SqlPmValueInterceptor() {
    }

    private static MinPartitionDateComponent getBean() {
        if (minPartitionDateComponent != null) {
            return minPartitionDateComponent;
        } else {
           // minPartitionDateComponent = (MinPartitionDateComponent)SpringContextUtils.getBean("minCreatedTimeComponent");
            return minPartitionDateComponent;
        }
    }

    @Override
    public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
        String sql = boundSql.getSql();
        String mSql = this.handlerPMValue(getReplaceParams(sql), sql);
        PluginUtils.mpBoundSql(boundSql).sql(mSql);
    }

    private String handlerPMValue(List<Map<String, String>> pmList, String sql) {
        Iterator var3 = pmList.iterator();

        while(var3.hasNext()) {
            Map<String, String> pm = (Map)var3.next();
            String type = pm.get("type");
            String ids = pm.get("ids");
            String timeStr = null;
            byte var9 = -1;
            switch(type.hashCode()) {
                case 111959959:
                    if (type.equals("vaa01")) {
                        var9 = 0;
                    }
                    break;
                case 111961881:
                    if (type.equals("vac01")) {
                        var9 = 1;
                    }
                    break;
                case 111963803:
                    if (type.equals("vae01")) {
                        var9 = 2;
                    }
            }

            switch(var9) {
                case 0:
                   // timeStr = getBean().getVaa1(ids);
                    break;
                case 1:
                  //  timeStr = getBean().getVac1(ids);
                    break;
                case 2:
                   // timeStr = getBean().getVae1(ids);
            }

            if (timeStr != null) {
                pm.put("value", timeStr);
                String target = ((String)pm.get("target")).replace("{timeValue}", timeStr);
                sql = sql.replace((CharSequence)pm.get("source"), target);
            }
        }

        return sql;
    }

    public static List<Map<String, String>> getReplaceParams(String sql) {
        Matcher m = p.matcher(sql);
        ArrayList pmList = new ArrayList();

        while(m.find()) {
            Map<String, String> params = new HashMap();
            String p1 = m.group(1);
            String p2 = m.group(2);
            String p3 = m.group(3);
            String[] pArray = p2.split("#");
            if (pArray.length == 2) {
                params.put("type", StringUtils.trimWhitespace(pArray[0]));
                params.put("ids", StringUtils.trimWhitespace(pArray[1]));
                String source = "/*#" + p1 + "'(" + p2 + ")'" + p3 + "#*/";
                params.put("p1", p1);
                params.put("p2", p2);
                params.put("source", source);
                params.put("target", p1 + "'{timeValue}'");
                pmList.add(params);
            }
        }

        return pmList;
    }

    static {
        p = Pattern.compile(pointReg);
    }
}
